﻿namespace Iit.Automation.Browsers.ConfigDataProvider
{
    using Shared;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;

    public class DataProvider
    {
        private static Dictionary<IitEnums.RegisteredBrowsers, DataProvider> _instances;
        private IitEnums.RegisteredBrowsers _whichBrowser;
        private string _driverFolderPath;

        static DataProvider()
        {
            _instances = new Dictionary<IitEnums.RegisteredBrowsers, DataProvider>();
        }

        private DataProvider(IitEnums.RegisteredBrowsers whichBrowser)
        {
            this._whichBrowser = whichBrowser;
        }

        public string DriverFolderPath
        {
            get
            {
                if (string.IsNullOrEmpty(this._driverFolderPath))
                {
                    this.GetDriverFolderPath();
                }

                return this._driverFolderPath;
            }
        }

        private void GetDriverFolderPath()
        {
            string folderPathKey = string.Format("{0}DriverFolderPath", this._whichBrowser.ToString());
            string folderPath = string.Empty;
            if (ConfigurationManager.AppSettings.AllKeys.Contains(folderPathKey))
            {
                if (ConfigurationManager.AppSettings[folderPathKey] != null)
                {
                    folderPath = Path.Combine(Environment.CurrentDirectory, "../../../", ConfigurationManager.AppSettings[folderPathKey]);
                }
            }

            if (string.IsNullOrEmpty(folderPath))
            {
                //ToDo: Need to log a warning here
                folderPath = Path.Combine(Environment.CurrentDirectory, string.Format("../../../Dependencies/{0}Driver", this._whichBrowser));
            }

            this._driverFolderPath = folderPath;
        }

        public static DataProvider GetInstance(IitEnums.RegisteredBrowsers whichBrowser)
        {
            if (!_instances.Keys.Contains(whichBrowser))
            {
                _instances.Add(whichBrowser, new DataProvider(whichBrowser));
            }

            return _instances[whichBrowser];
        }
    }
}
